<?php
class lib_sys_access extends lib_base{
	public $table_name = "qc_sys_access";
    public $pk = "id";

    public function getList($cond)
    {
        $all = $this->findAll(array($cond),'sort asc,id asc');
        $all = formatChildData($all);
        return $all;
    }

    public function get_treeList($cond,$sort='sort asc,id desc')
    {
        $all = $this->findAll(array($cond),$sort);
        $all = formatTreeData($all);
        return $all;
    }

    public function get_groupId($type=1,$url)
    {
        $cond = "type={$type} and url='{$url}'";
        $info = $this->find(array($cond));
        if($info['parent_id']){
            $p_info = $this->findById($info['parent_id']);
            if($p_info['parent_id']){
                $pp_info = $this->findById($p_info['parent_id']);
                return $pp_info['id'];
            }
            return $p_info['id'];
        }
        return $info['id'];
    }

    public function get_menuList($menus,$group,$routeUri){
        // dump($this->group);
        foreach ($menus  as $k1 => $v1) {
            if($k1 == $group){
                $menus[$k1]['active'] = 1;
            }
            $firstUris = [$v1['url']];
            !isset($menus[$k1]['active'])
            && $menus[$k1]['active'] = in_array($routeUri, $firstUris);
            // 遍历：二级菜单
            if (isset($v1['children'])) {
                foreach ($v1['children'] as $k2 => $v2) {
                    $secondUris = [$v2['url']];
                    // 三级菜单所有uri
                    if (isset($v2['children'])) {
                        foreach ($v2['children'] as $k3 => $v3) {
                            $secondUris = array_merge($secondUris,[$v3['url']]);
                        }
                    }
                    // 二级菜单：active
                    !isset($menus[$k1]['children'][$k2]['active'])
                    && $menus[$k1]['children'][$k2]['active'] = in_array($routeUri, $secondUris);
                }
            }
        }
        return $menus;
    }

}